<?php

namespace app\topics\middle;

class ListMiddle extends \app\base\middle\BaseMiddle {


    protected function meta() {
        $this->setMeta('社区');
        $this->setName('社区');
        $this->setCrumb([
            [
                'name' => '社区',
                'url' => url()
            ]
        ]);

        return $this->run([
            'pageInfo' => $this->pageInfo
        ]);
    }

    protected function data() {
        $filter = $this->params['filter'];
        $tag = urldecode($this->params['tag']);
        $tag = html_clear($tag);
        $pageParams = [];
        $where = [];
        $order = 'A.update_time desc';
        switch ($filter) {
            case 1:
                $where['A.attr_excellent'] = 1;
                break;
            case 2:
                $where['A.attr_top'] = 1;
                break;
            case 3:
                $order = 'A.create_time desc';
                break;
            default:
                $filter = 0;
                break;
        }
        $pageParams['filter'] = $filter;

        $tagInfo = [];
        if($tag) {
            $tagInfo = target('topics/TopicsTags')->getWhereInfo([
                'name' => $tag
            ]);
            if(empty($tagInfo)) {
                return $this->stop('标签内容不存在!');
            }
            $where['_sql'] = 'A.detail_id in ('.$tagInfo['detail_ids'].')';
        }

        $pageLimit = 20;
        $model = target('topics/TopicsDetail');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit);
        $list = $model->loadList($where, $pageData['limit'], 'A.attr_top desc' . ($order ? ','.$order : ''));

        return $this->run([
            'pageData' => $pageData,
            'countList' => $count,
            'pageList' => $list,
            'tagInfo' => $tagInfo,
            'pageParams' => $pageParams
        ]);
    }


}